package winnu.dao;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;

  
  
/**
 * This class was autogenerated by Torque on:
 *
 * [Mon Mar 01 17:13:14 SGT 2010]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to Item
 */
public abstract class BaseItem extends BaseObject
{
    /** The Peer class */
    private static final ItemPeer peer =
        new ItemPeer();

        
    /** The value for the itemId field */
    private int itemId;
      
    /** The value for the genericName field */
    private String genericName;
      
    /** The value for the brandName field */
    private String brandName;
      
    /** The value for the classification field */
    private String classification;
      
    /** The value for the type field */
    private String type;
      
    /** The value for the minimumSupplyLimit field */
    private int minimumSupplyLimit;
      
    /** The value for the userId field */
    private int userId;
      
    /** The value for the nextBatch field */
    private int nextBatch;
  
    
    /**
     * Get the ItemId
     *
     * @return int
     */
    public int getItemId()
    {
        return itemId;
    }

                                              
    /**
     * Set the value of ItemId
     *
     * @param v new value
     */
    public void setItemId(int v) throws TorqueException
    {
    
                  if (this.itemId != v)
              {
            this.itemId = v;
            setModified(true);
        }
    
          
                                  
        // update associated ItemBatch
        if (collItemBatchs != null)
        {
            for (int i = 0; i < collItemBatchs.size(); i++)
            {
                ((ItemBatch) collItemBatchs.get(i))
                    .setItemId(v);
            }
        }
                      }
  
    /**
     * Get the GenericName
     *
     * @return String
     */
    public String getGenericName()
    {
        return genericName;
    }

                        
    /**
     * Set the value of GenericName
     *
     * @param v new value
     */
    public void setGenericName(String v) 
    {
    
                  if (!ObjectUtils.equals(this.genericName, v))
              {
            this.genericName = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the BrandName
     *
     * @return String
     */
    public String getBrandName()
    {
        return brandName;
    }

                        
    /**
     * Set the value of BrandName
     *
     * @param v new value
     */
    public void setBrandName(String v) 
    {
    
                  if (!ObjectUtils.equals(this.brandName, v))
              {
            this.brandName = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the Classification
     *
     * @return String
     */
    public String getClassification()
    {
        return classification;
    }

                        
    /**
     * Set the value of Classification
     *
     * @param v new value
     */
    public void setClassification(String v) 
    {
    
                  if (!ObjectUtils.equals(this.classification, v))
              {
            this.classification = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the Type
     *
     * @return String
     */
    public String getType()
    {
        return type;
    }

                        
    /**
     * Set the value of Type
     *
     * @param v new value
     */
    public void setType(String v) 
    {
    
                  if (!ObjectUtils.equals(this.type, v))
              {
            this.type = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the MinimumSupplyLimit
     *
     * @return int
     */
    public int getMinimumSupplyLimit()
    {
        return minimumSupplyLimit;
    }

                        
    /**
     * Set the value of MinimumSupplyLimit
     *
     * @param v new value
     */
    public void setMinimumSupplyLimit(int v) 
    {
    
                  if (this.minimumSupplyLimit != v)
              {
            this.minimumSupplyLimit = v;
            setModified(true);
        }
    
          
              }
  
    /**
     * Get the UserId
     *
     * @return int
     */
    public int getUserId()
    {
        return userId;
    }

                              
    /**
     * Set the value of UserId
     *
     * @param v new value
     */
    public void setUserId(int v) throws TorqueException
    {
    
                  if (this.userId != v)
              {
            this.userId = v;
            setModified(true);
        }
    
                          
                if (aUser != null && !(aUser.getUserId() == v))
                {
            aUser = null;
        }
      
              }
  
    /**
     * Get the NextBatch
     *
     * @return int
     */
    public int getNextBatch()
    {
        return nextBatch;
    }

                        
    /**
     * Set the value of NextBatch
     *
     * @param v new value
     */
    public void setNextBatch(int v) 
    {
    
                  if (this.nextBatch != v)
              {
            this.nextBatch = v;
            setModified(true);
        }
    
          
              }
  
      
    
                  
    
        private User aUser;

    /**
     * Declares an association between this object and a User object
     *
     * @param v User
     * @throws TorqueException
     */
    public void setUser(User v) throws TorqueException
    {
            if (v == null)
        {
                    setUserId(0);
                  }
        else
        {
            setUserId(v.getUserId());
        }
                aUser = v;
    }

                                            
    /**
     * Get the associated User object
     *
     * @return the associated User object
     * @throws TorqueException
     */
    public User getUser() throws TorqueException
    {
        if (aUser == null && (this.userId > 0))
        {
                          aUser = UserPeer.retrieveByPK(SimpleKey.keyFor(this.userId));
              
            /* The following can be used instead of the line above to
               guarantee the related object contains a reference
               to this object, but this level of coupling
               may be undesirable in many circumstances.
               As it can lead to a db query with many results that may
               never be used.
               User obj = UserPeer.retrieveByPK(this.userId);
               obj.addItems(this);
            */
        }
        return aUser;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey.  e.g.
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
           */
    public void setUserKey(ObjectKey key) throws TorqueException
    {
      
                        setUserId(((NumberKey) key).intValue());
                  }
       
                                
            
    /**
     * Collection to store aggregation of collItemBatchs
     */
    protected List collItemBatchs;

    /**
     * Temporary storage of collItemBatchs to save a possible db hit in
     * the event objects are add to the collection, but the
     * complete collection is never requested.
     */
    protected void initItemBatchs()
    {
        if (collItemBatchs == null)
        {
            collItemBatchs = new ArrayList();
        }
    }

    /**
     * Method called to associate a ItemBatch object to this object
     * through the ItemBatch foreign key attribute
     *
     * @param l ItemBatch
     * @throws TorqueException
     */
    public void addItemBatch(ItemBatch l) throws TorqueException
    {
        getItemBatchs().add(l);
        l.setItem((Item) this);
    }

    /**
     * The criteria used to select the current contents of collItemBatchs
     */
    private Criteria lastItemBatchsCriteria = null;

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getItemBatchs(new Criteria())
     *
     * @throws TorqueException
     */
    public List getItemBatchs() throws TorqueException
    {
        if (collItemBatchs == null)
        {
            collItemBatchs = getItemBatchs(new Criteria(10));
        }
        return collItemBatchs;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Item has previously
     * been saved, it will retrieve related ItemBatchs from storage.
     * If this Item is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     *
     * @throws TorqueException
     */
    public List getItemBatchs(Criteria criteria) throws TorqueException
    {
        if (collItemBatchs == null)
        {
            if (isNew())
            {
               collItemBatchs = new ArrayList();
            }
            else
            {
                      criteria.add(ItemBatchPeer.ITEMID, getItemId() );
                      collItemBatchs = ItemBatchPeer.doSelect(criteria);
            }
        }
        else
        {
            // criteria has no effect for a new object
            if (!isNew())
            {
                // the following code is to determine if a new query is
                // called for.  If the criteria is the same as the last
                // one, just return the collection.
                      criteria.add(ItemBatchPeer.ITEMID, getItemId());
                      if (!lastItemBatchsCriteria.equals(criteria))
                {
                    collItemBatchs = ItemBatchPeer.doSelect(criteria);
                }
            }
        }
        lastItemBatchsCriteria = criteria;

        return collItemBatchs;
    }

    /**
     * If this collection has already been initialized, returns
     * the collection. Otherwise returns the results of
     * getItemBatchs(new Criteria(),Connection)
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getItemBatchs(Connection con) throws TorqueException
    {
        if (collItemBatchs == null)
        {
            collItemBatchs = getItemBatchs(new Criteria(10), con);
        }
        return collItemBatchs;
    }

    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Item has previously
     * been saved, it will retrieve related ItemBatchs from storage.
     * If this Item is new, it will return
     * an empty collection or the current collection, the criteria
     * is ignored on a new object.
     * This method takes in the Connection also as input so that
     * referenced objects can also be obtained using a Connection
     * that is taken as input
     */
    public List getItemBatchs(Criteria criteria, Connection con)
            throws TorqueException
    {
        if (collItemBatchs == null)
        {
            if (isNew())
            {
               collItemBatchs = new ArrayList();
            }
            else
            {
                       criteria.add(ItemBatchPeer.ITEMID, getItemId());
                       collItemBatchs = ItemBatchPeer.doSelect(criteria, con);
             }
         }
         else
         {
             // criteria has no effect for a new object
             if (!isNew())
             {
                 // the following code is to determine if a new query is
                 // called for.  If the criteria is the same as the last
                 // one, just return the collection.
                       criteria.add(ItemBatchPeer.ITEMID, getItemId());
                       if (!lastItemBatchsCriteria.equals(criteria))
                 {
                     collItemBatchs = ItemBatchPeer.doSelect(criteria, con);
                 }
             }
         }
         lastItemBatchsCriteria = criteria;

         return collItemBatchs;
     }

                        
              
                    
                    
                                
                                                              
                                        
                    
                    
          
    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Item is new, it will return
     * an empty collection; or if this Item has previously
     * been saved, it will retrieve related ItemBatchs from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Item.
     */
    protected List getItemBatchsJoinSupplier(Criteria criteria)
        throws TorqueException
    {
        if (collItemBatchs == null)
        {
            if (isNew())
            {
               collItemBatchs = new ArrayList();
            }
            else
            {
                            criteria.add(ItemBatchPeer.ITEMID, getItemId());
                            collItemBatchs = ItemBatchPeer.doSelectJoinSupplier(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
                        criteria.add(ItemBatchPeer.ITEMID, getItemId());
                        if (!lastItemBatchsCriteria.equals(criteria))
            {
                collItemBatchs = ItemBatchPeer.doSelectJoinSupplier(criteria);
            }
        }
        lastItemBatchsCriteria = criteria;

        return collItemBatchs;
    }
                  
                    
                              
                                
                                                              
                                        
                    
                    
          
    /**
     * If this collection has already been initialized with
     * an identical criteria, it returns the collection.
     * Otherwise if this Item is new, it will return
     * an empty collection; or if this Item has previously
     * been saved, it will retrieve related ItemBatchs from storage.
     *
     * This method is protected by default in order to keep the public
     * api reasonable.  You can provide public methods for those you
     * actually need in Item.
     */
    protected List getItemBatchsJoinItem(Criteria criteria)
        throws TorqueException
    {
        if (collItemBatchs == null)
        {
            if (isNew())
            {
               collItemBatchs = new ArrayList();
            }
            else
            {
                            criteria.add(ItemBatchPeer.ITEMID, getItemId());
                            collItemBatchs = ItemBatchPeer.doSelectJoinItem(criteria);
            }
        }
        else
        {
            // the following code is to determine if a new query is
            // called for.  If the criteria is the same as the last
            // one, just return the collection.
            boolean newCriteria = true;
                        criteria.add(ItemBatchPeer.ITEMID, getItemId());
                        if (!lastItemBatchsCriteria.equals(criteria))
            {
                collItemBatchs = ItemBatchPeer.doSelectJoinItem(criteria);
            }
        }
        lastItemBatchsCriteria = criteria;

        return collItemBatchs;
    }
                            


          
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
              fieldNames.add("ItemId");
              fieldNames.add("GenericName");
              fieldNames.add("BrandName");
              fieldNames.add("Classification");
              fieldNames.add("Type");
              fieldNames.add("MinimumSupplyLimit");
              fieldNames.add("UserId");
              fieldNames.add("NextBatch");
              fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
          if (name.equals("ItemId"))
        {
                return new Integer(getItemId());
            }
          if (name.equals("GenericName"))
        {
                return getGenericName();
            }
          if (name.equals("BrandName"))
        {
                return getBrandName();
            }
          if (name.equals("Classification"))
        {
                return getClassification();
            }
          if (name.equals("Type"))
        {
                return getType();
            }
          if (name.equals("MinimumSupplyLimit"))
        {
                return new Integer(getMinimumSupplyLimit());
            }
          if (name.equals("UserId"))
        {
                return new Integer(getUserId());
            }
          if (name.equals("NextBatch"))
        {
                return new Integer(getNextBatch());
            }
          return null;
    }
    
    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
          if (name.equals(ItemPeer.ITEMID))
        {
                return new Integer(getItemId());
            }
          if (name.equals(ItemPeer.GENERICNAME))
        {
                return getGenericName();
            }
          if (name.equals(ItemPeer.BRANDNAME))
        {
                return getBrandName();
            }
          if (name.equals(ItemPeer.CLASSIFICATION))
        {
                return getClassification();
            }
          if (name.equals(ItemPeer.TYPE))
        {
                return getType();
            }
          if (name.equals(ItemPeer.MINIMUMSUPPYLIMIT))
        {
                return new Integer(getMinimumSupplyLimit());
            }
          if (name.equals(ItemPeer.USERID))
        {
                return new Integer(getUserId());
            }
          if (name.equals(ItemPeer.NEXTBATCH))
        {
                return new Integer(getNextBatch());
            }
          return null;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
            if (pos == 0)
        {
                return new Integer(getItemId());
            }
              if (pos == 1)
        {
                return getGenericName();
            }
              if (pos == 2)
        {
                return getBrandName();
            }
              if (pos == 3)
        {
                return getClassification();
            }
              if (pos == 4)
        {
                return getType();
            }
              if (pos == 5)
        {
                return new Integer(getMinimumSupplyLimit());
            }
              if (pos == 6)
        {
                return new Integer(getUserId());
            }
              if (pos == 7)
        {
                return new Integer(getNextBatch());
            }
              return null;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
          save(ItemPeer.getMapBuilder()
                .getDatabaseMap().getName());
      }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
       * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
       *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
          try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
      }

      /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
      /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
          if (!alreadyInSave)
        {
            alreadyInSave = true;


  
            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    ItemPeer.doInsert((Item) this, con);
                    setNew(false);
                }
                else
                {
                    ItemPeer.doUpdate((Item) this, con);
                }
            }

                                      
                
            if (collItemBatchs != null)
            {
                for (int i = 0; i < collItemBatchs.size(); i++)
                {
                    ((ItemBatch) collItemBatchs.get(i)).save(con);
                }
            }
                          alreadyInSave = false;
        }
      }


                          
      /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param  itemId ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        throws TorqueException
    {
            setItemId(((NumberKey) key).intValue());
        }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
            setItemId(Integer.parseInt(key));
        }

  
    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
          return SimpleKey.keyFor(getItemId());
      }

 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
       * It then fills all the association collections and sets the
     * related objects to isNew=true.
       */
      public Item copy() throws TorqueException
    {
        return copyInto(new Item());
    }
  
    protected Item copyInto(Item copyObj) throws TorqueException
    {
          copyObj.setItemId(itemId);
          copyObj.setGenericName(genericName);
          copyObj.setBrandName(brandName);
          copyObj.setClassification(classification);
          copyObj.setType(type);
          copyObj.setMinimumSupplyLimit(minimumSupplyLimit);
          copyObj.setUserId(userId);
          copyObj.setNextBatch(nextBatch);
  
                    copyObj.setItemId(0);
                                                      
                                      
                
        List v = getItemBatchs();
        for (int i = 0; i < v.size(); i++)
        {
            ItemBatch obj = (ItemBatch) v.get(i);
            copyObj.addItemBatch(obj.copy());
        }
                    
        return copyObj;
    }

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public ItemPeer getPeer()
    {
        return peer;
    }

    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Item:\n");
        str.append("ItemId = ")
           .append(getItemId())
           .append("\n");
        str.append("GenericName = ")
           .append(getGenericName())
           .append("\n");
        str.append("BrandName = ")
           .append(getBrandName())
           .append("\n");
        str.append("Classification = ")
           .append(getClassification())
           .append("\n");
        str.append("Type = ")
           .append(getType())
           .append("\n");
        str.append("MinimumSupplyLimit = ")
           .append(getMinimumSupplyLimit())
           .append("\n");
        str.append("UserId = ")
           .append(getUserId())
           .append("\n");
        str.append("NextBatch = ")
           .append(getNextBatch())
           .append("\n");
        return(str.toString());
    }
}
